require("../../assets/css/index.less")
require("../../assets/css/common.less");
require("./prj_video.less")
require("../../assets/iconfont/iconfont.css")

const request = require("../../lib/requst")
function $(select) {
    return document.querySelector(select)
}

let API = "http://fitness.h5.itsource.cn:3701"
let fragments = JSON.parse(localStorage.getItem("fragments"));
window.onload = () => {
    $(".video").src = API + fragments[0].videoUrl
    $(".v_sum").textContent = fragments.length;
    let timeList = [];//存运动总时间
    let time = 0 //运动时间
    //是否播放	
    function play() {
        if (biaogan) {
            video.play();
            $(".player").classList.remove("icon-play")
            $(".player").classList.add("icon-zanting")
            $(".mask").style.display = "none"
        } else {
            video.pause();
            $(".player").classList.remove("icon-zanting")
            $(".player").classList.add("icon-play")
            $(".mask").style.display = "block"
        }
        biaogan = !biaogan;
    }
    // 播放暂停按钮
    let biaogan = true;
    $(".player").addEventListener("click", function () {
        play();
        // console.log($(".video").currentTime,times)
    })
    $(".continue").addEventListener("click",function(){
        play();
    })
    //下一曲
    let index = 0
    $(".next").addEventListener("click", function () {
        index++;
        if(index<0){
            index= (fragments.length-1)+index%fragments.length
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent =fragments[index].title
        }else{
            index=index % fragments.length;
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent =fragments[index].title
        }
        biaogan = true;
        play();
        timeList.push(time)
        console.log(timeList)
    })
    //上一曲
    $(".pre").addEventListener("click", function () {
        index--;
        if(index<0){
            index= fragments.length+index%fragments.length
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent =fragments[index].title
        }else{
            index=index % fragments.length;
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent =fragments[index].title
        }
        biaogan = true;
        play();
        timeList.push(time)
        console.log(timeList)
    })
    //进度条
    video.addEventListener("timeupdate",function(){
        $(".progress").style.width = video.currentTime / video.duration * $(".pro_box").clientWidth + `px`;

        time = video.currentTime;

    })
    // 播完事件
    // 自动播放下一曲
    video.addEventListener("ended",function(){
        index++;
        if(index<0){
            index= (fragments.length-1)+index%fragments.length
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent = fragments[index].title
        }else{
            index=index % fragments.length;
            $(".video").src = API + fragments[index].videoUrl
            $(".dangqian").textContent = index+1;
            $(".text2").textContent = fragments[index].title
        }
        biaogan = true;
        play();
    })
    $(".endtrain").addEventListener("click",function(){
           timeList.push($(".video").currentTime);
        //    计算总时长
        let alltotal = timeList.reduce((total,cur)=>{
            return total+cur
        },0)
           console.log(timeList)
           request.post("/api/exercise/save",{
            type:2,
            duration :alltotal,
            calorie: alltotal*1000,
           }).then(res=>{
               location.href = "./course.html"
           })
    })


}
